package com.extension.company.admin.wxapi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;

import com.extension.company.KEY;
import com.extension.company.MyApplication;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.eventbus.WXLoginCode;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.weixin.view.WXCallbackActivity;
import com.umeng.weixin.umengwx.b;

import org.greenrobot.eventbus.EventBus;

/**
 * zwk
 * 功能：友盟的分享的回调 extends WXCallbackActivity ；
 * 而原生的微信的登录回调和友盟回调名称一样，由于友盟也是继承的微信的方法，故可以复用。
 * 实现微信的接口 IWXAPIEventHandler
 */
public class WXEntryActivity extends WXCallbackActivity implements IWXAPIEventHandler {
	private static final int RETURN_MSG_TYPE_LOGIN = 1;  // 登录
	private static final int RETURN_MSG_TYPE_SHARE = 2;  // 分享
	private static IWXAPI iwxapi;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		iwxapi = WXAPIFactory.createWXAPI(this, KEY.WX_PAY_ID);
		iwxapi.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		//如果没回调onResp，八成是这句没有写
		iwxapi.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq baseReq) {

	}

	@Override
	public void onResp(BaseResp baseResp) {
		switch (baseResp.errCode) {
			case BaseResp.ErrCode.ERR_USER_CANCEL:// 取消登录
				if (baseResp.getType() == RETURN_MSG_TYPE_SHARE) {
					Log.d("zwk", "------分享取消----");
				}
				if (baseResp.getType() == RETURN_MSG_TYPE_LOGIN) {
					Log.d("zwk", "------登录取消----");

				}
				finish();
				break;
			case BaseResp.ErrCode.ERR_OK://登录成功

				if (baseResp.getType() == RETURN_MSG_TYPE_LOGIN) { // 登录
					Bundle bundle = getIntent().getExtras();
					SendAuth.Resp resp = new SendAuth.Resp(bundle);
					String code = resp.code;
					Log.d("zwk", "-----code----:" + code);
					// 进行网路请求
					if (!TextUtils.isEmpty(code)) {
						EventBus.getDefault().post(new WXLoginCode(code));
					} else {
						ToastUtil.showToast(this, "微信授权失败");
					}
				}

				break;
			default:
				break;


		}

	}


	@Override
	public void a(com.umeng.weixin.umengwx.b b) { // 执行友盟的回调方法
		//super.a(b);
	}

	@Override
	protected void a(Intent intent) {
		super.a(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		hintKeyboard();
	}


	private void hintKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive() && getCurrentFocus() != null) {
			if (getCurrentFocus().getWindowToken() != null) {
				imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

}
